package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Orderlist;
import com.itheima.service.OrdersettinglistService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/orderlist")
public class OrdersettinglistController {

    @Reference
    private OrdersettinglistService service;

//orderDate: "2019-11-13T16:00:00.000Z"
//orderStatus: "未预约"
//orderType: "微信预约"
//setmeal: "堕胎"
//telephone: "18792084140"
//username: "啊"
    //预约新增
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,Object> map) {
        try {
            Result result=  service.add(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }


    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody Map map) throws Exception {
        System.out.println(map);
        PageResult pageResult = service.pageQuery(map);

        return pageResult;
    }

    @RequestMapping("/findById")
    public Result findByphoneNumber(Integer id){
        try{
            Orderlist orderlist = service.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,orderlist);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Orderlist orderlist){
        try {
          Result result=  service.edit(orderlist);
          return result;
        }catch (Exception e){
            return new Result(false,"编辑订单失败");
        }

    }
    //删除
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('ORDER_DELETE')")//权限校验
    public Result delete(Integer id){
        try {
            service.delete(id);
        }catch (Exception e){
            return new Result(false, "删除订单失败");
        }
        return new Result(true,"删除订单成功");
    }
    //修改就诊状态
    @RequestMapping("/editstatus")

    public Result editstatus(@RequestBody Orderlist orderlist){//修改后的值
        try {
            service.editstatus(orderlist);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"修改状态失败");
        }
        return new Result(true,"修改状态成功");
    }
}
